package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: bowen
 * @Date: 2019/8/7 9:43
 * @Description:
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

	@Autowired
	MemberDao memberDao;

	@Autowired
	OrderDao orderDao;

	/**
	 *  @描述：获取运营数据
	 */
	public Map<String, Object> getBusinessReportData() {
		Map<String, Object> map = new HashMap<>();
		//reportDate:null,
		//		todayNewMember :0,
		//		totalMember :0,
		//		thisWeekNewMember :0,
		//		thisMonthNewMember :0,
		//		todayOrderNumber :0,
		//		todayVisitsNumber :0,
		//		thisWeekOrderNumber :0,
		//		thisWeekVisitsNumber :0,
		//		thisMonthOrderNumber :0,
		//		thisMonthVisitsNumber :0,
		//		hotSetmeal :[
		//{name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
		//{name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
        //            ]

		// 0.准备日期字符串（当前日期，本周一对应的日期，本月一号对应的日期）
		LocalDate now = LocalDate.now();
		String today = getDateString(now);
		String monday = getDateString(now.with(DayOfWeek.MONDAY));
		String monthOne = getDateString(now.with(TemporalAdjusters.firstDayOfMonth()));
		//  1. 获取会员数据
		Integer todayNewMember = memberDao.findMemberCountByDate(today);
		Integer totalMember = memberDao.findMemberTotalCount();
		Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(monday);
		Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(monthOne);

		//  2. 获取预约数
		Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
		Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
		Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(monday);
		Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(monday);
		Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(monthOne);
		Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(monthOne);
		//  3. 获取热门套餐
		List<Map> hotSetmeal = orderDao.findHotSetmeal();

		map.put("reportDate",today);
		map.put("todayNewMember",todayNewMember);
		map.put("totalMember",totalMember);
		map.put("thisWeekNewMember",thisWeekNewMember);
		map.put("thisMonthNewMember",thisMonthNewMember);
		map.put("todayOrderNumber",todayOrderNumber);
		map.put("thisWeekOrderNumber",thisWeekOrderNumber);
		map.put("thisMonthOrderNumber",thisMonthOrderNumber);
		map.put("todayVisitsNumber",todayVisitsNumber);
		map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
		map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
		map.put("hotSetmeal",hotSetmeal);
		return map;
	}
   
	/**
	 *  @描述：获取日期字符串
	 */
	private static String getDateString(LocalDate now) {
		return DateTimeFormatter.ofPattern("yyyy-MM-dd").format(now);
	}

	public static void main(String[] args) {
		LocalDate now = LocalDate.now();
		String today = getDateString(now);
		System.out.println(today);
	}
}
